package cn.wit.baoyan.service.impl;

import cn.wit.baoyan.condition.BaoyanCondition;
import cn.wit.baoyan.mapper.BaoyanMapper;
import cn.wit.utils.pojo.Baoyan;
import cn.wit.baoyan.service.BaoyanService;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class BaoyanServiceImpl implements BaoyanService {
    @Autowired
    private BaoyanMapper baoyanMapper;

    @Override
    public Result insertBaoyan(Baoyan baoyan) {
        int insert = baoyanMapper.insert(baoyan);
        if (insert > 0) {
            return Result.ok();
        }
        return Result.error("新增失败");
    }

    @Override
    public Result updateBaoyan(Baoyan baoyan) {
        int insert = baoyanMapper.updateById(baoyan);
        if (insert > 0) {
            return Result.ok();
        }
        return Result.error("更新失败");
    }

    @Override
    public Result deleteBaoyan(Integer id) {
        int insert = baoyanMapper.deleteById(id);
        if (insert > 0) {
            return Result.ok();
        }
        return Result.error("删除失败");
    }

    @Override
    public Result selectById(Integer id) {
        Baoyan baoyan = baoyanMapper.selectById(id);
        return Result.ok(baoyan);
    }

    @Override
    public Result searchBaoyan(IPage<Baoyan> page, BaoyanCondition condition) {
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String now = sdf.format(date);
        QueryWrapper<Baoyan> wrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(condition.getCategory())) {
            wrapper.like("category", condition.getCategory());
        }
        if (!StringUtils.isBlank(condition.getSchool())) {
            wrapper.like("school", condition.getSchool());
        }
        if (!StringUtils.isBlank(condition.getInstitute())) {
            wrapper.like("institute", condition.getInstitute());
        }
        if(condition.getState()!=null){
            if(condition.getState().equals("1")){
                //报名未开始
                wrapper.gt("entry_begin_time",now);
            }else if(condition.getState().equals("2")){
                //报名中
                wrapper.le("entry_begin_time",now);
                wrapper.gt("entry_end_time",now);
            }else if(condition.getState().equals("3")){
                //报名结束 但未开始活动
                wrapper.le("entry_end_time",now);
                wrapper.gt("begin_time",now);
            }else if(condition.getState().equals("4")){
                //活动中但未结束
                wrapper.le("begin_time",now);
                wrapper.gt("end_time",now);
            }else if(condition.getState().equals("5")){
                wrapper.le("end_time",now);
            }
        }
//        if (!StringUtils.isBlank(condition.getBeginTime()) && !StringUtils.isBlank(condition.getEndTime())) {
//            wrapper.gt("begin_time", condition.getBeginTime());
//            wrapper.lt("end_time", condition.getEndTime());
//        }
//        if (!StringUtils.isBlank(condition.getEntryBeginTime()) && !StringUtils.isBlank(condition.getEntryEndTime()) ) {
//            wrapper.gt("entry_begin_time", condition.getEntryBeginTime());
//            wrapper.lt("entry_end_time", condition.getEntryEndTime());
//        }
        IPage<Baoyan> baoyanIPage = baoyanMapper.selectPage(page, wrapper);
        return Result.ok(baoyanIPage);
    }

    @Override
    public Result excelImport(MultipartFile file) {
        InputStream inputStream = null;
        int num=0;
        try {
            inputStream=file.getInputStream();
            Workbook workbook= WorkbookFactory.create(inputStream);//获得工作簿
            Sheet sheet=workbook.getSheetAt(0);//获得工作空间
            int numRows = sheet.getPhysicalNumberOfRows();
            for(int i=1;i<numRows;i++){
                Row row = sheet.getRow(i);
                Baoyan baoyan=new Baoyan();
                String category = row.getCell(0,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();//保研类别
                if(category!=null&&category!=""){
                    baoyan.setCategory(category);
                }
                String school = row.getCell(1,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();//保研学校
                if(school!=null && school!=""){
                    baoyan.setSchool(school);
                }
                String institute = row.getCell(2,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();//保研学校
                if(institute!=null&&institute!=""){
                    baoyan.setInstitute(institute);
                }
                if(row.getCell(3,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(row.getCell(3,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK))){
                    // 转换日期/时间为字符串格式
                    Date entry_begin_time = row.getCell(3, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
                    baoyan.setEntryBeginTime(entry_begin_time);
                }
                if(row.getCell(4,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(row.getCell(4,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK))){
                    // 转换日期/时间为字符串格式
                    Date entry_end_time = row.getCell(4, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
                    baoyan.setEntryEndTime(entry_end_time);
                }
                if(row.getCell(5,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(row.getCell(5,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK))){
                    // 转换日期/时间为字符串格式
                    Date begin_time = row.getCell(4, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
                    baoyan.setBeginTime(begin_time);
                }
                if(row.getCell(6,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(row.getCell(6,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK))){
                    // 转换日期/时间为字符串格式
                    Date end_time = row.getCell(6, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
                    baoyan.setEndTime(end_time);
                }
                String introduce = row.getCell(7,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
                if(introduce!=null && introduce!=""){
                    baoyan.setIntroduce(introduce);
                }
                String link = row.getCell(8,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
                if(link!=null &&link!=""){
                    baoyan.setLink(link);
                }
                int count=baoyanMapper.insert(baoyan);
                num+=count;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(num>0){
            return Result.ok();
        }else {
            return Result.error("导入数据失败");
        }
    }
}
